<?
function relative_date($date)
{
		$relative = " ";
		$todate = date("Y-m-d");
		list ($toyear, $tomonth, $today) = explode("-",$todate);
		list($year, $month, $day) = explode("-",$date);
		if ($toyear - $year < 0)
		{
			if ($year - $toyear > 1)
				$relative =  "in " . ($year - $toyear) . " years";
			else
			{
				$relative = "in " . 12 - $tomonth + $month . "months";
			}
		}
		elseif ($toyear - $year == 0)
		{
			if ($tomonth - $month > 0)
			{
				if ($tomonth - $month > 1)
					$relative = $tomonth - $month . " months ago";
				else
					$relative = "last month";
			}
			elseif ($tomonth - $month == 0)
			{
				if ($today - $day > 0)
				{
					if ($today - $day == 1)
						$relative = "yesterday";
					else
						$relative = $today - $day . " days ago";
						
				}
				elseif ($today - $day == 0)
					$relative = "today";
				else
					if ($day - $today > 1)
					{
						$relative = $day - $today . " days";
						$relative = "in " . $relative;
					}
					else
						$relative = "tomorrow";
			}
			else
			{
				if ($month - $tomonth > 1)
				{
					$relative = $month - $tomonth . " months";
					$relative = "in " . $relative;
				}
				else
					$relative = "in next month";
			}
		}
		else
		{
			if ($toyear - $year == 1)
				$relative = " last year";
			else
				$relative = $toyear - $year . " years ago";
		}
		return $relative;
}

function format_and_extend_date($date)
{
	list($year,$month,$day) = explode("-",$date);
	$relative = relative_date($date);
	if ($day < 10)
		$day=$day[1];
	if ($day == "1" or $day == "21" or $day == "31") $day = $day . "st";
	elseif ($day == "2" or $day == "22") $day = $day . "nd";
	elseif ($day == "3" or $day == "23") $day = $day . "rd";
	else $day = $day . "th";
	switch ($month){
	case 1:
		$month = "January";
		break;
	case 2:
		$month = "February";
		break;
	case 3:
		$month = "March";
		break;
	case 4:
		$month = "April";
		break;
	case 5:
		$month = "May";
		break;
	case 6:
		$month = "June";
		break;
	case 7:
		$month = "July";
		break;
	case 8:
		$month = "August";
		break;
	case 9:
		$month = "September";
		break;
	case 10:
		$month = "October";
		break;
	case 11:
		$month = "November";
		break;
	case 12:
		$month = "December";
		break;
	}
	
	$newdate = $day . " " . $month . " " . $year;
	return $newdate . " ($relative)";
}

